﻿using System;
using System.Linq;
using System.Collections.Generic;
using Citi.Scheduler.Domain.Utils;
using System.Data.Objects.DataClasses;
using Citi.Helpers;
using System.Configuration;
using System.IO;

namespace Citi.Scheduler.Domain
{
    public partial class RoutineExecutionHistory
    {
        private static string WaitingExecutionPath { get { return ConfigurationManager.AppSettings["WaitingExecutionPath"]; } }
        private static string NetworkUser { get { return ConfigurationManager.AppSettings["NetworkUser"]; } }
        private static string NetWorkPassword { get { return ConfigurationManager.AppSettings["NetWorkPassword"]; } }

        public static RoutineExecutionStatus GetLastStatus(int routineExecutionId)
        {
            var query = string.Format(@"SELECT TOP 1 Status FROM RoutineExecutionHistory 
                                        WHERE RoutineExecutionId = {0} 
                                        ORDER BY Date DESC", routineExecutionId);

            var datatable = SqlHelper.ExecuteReader(query, ConnectionString, 3000);

            return (RoutineExecutionStatus)(datatable.Rows.Count > 0 ? int.Parse(datatable.Rows[0][0].ToString()) : 0);
        }

        public static RoutineExecutionHistory InsertHistory(int executionId, RoutineExecutionStatus status, string details = null)
        {
            var executionHistory = new RoutineExecutionHistory
            {
                Date = DateTime.Now,
                RoutineExecutionId = executionId,
                Status = (short)status,
                Details = details
            };

            var i = 0;
            while (executionHistory.Id == 0)
            {
                i++;
                executionHistory.Save(executionHistory);

                if (i > 100) break;
            }

            if (executionHistory.Id != 0)
                UpdateWaitingExecutionFolder(status, executionId);

            return executionHistory;
        }

        public static void UpdateWaitingExecutionFolder(RoutineExecutionStatus status, int executionId)
        {
            try
            {
                var filePath = string.Format(WaitingExecutionPath, executionId);

                if (status == RoutineExecutionStatus.WaitingFreeMachine || status == RoutineExecutionStatus.WaitingSpecificFreeMachine)
                    CreateWaitingExecutionFile(executionId);
                else
                    DeleteWaitingExecutionFile(executionId);
            }
            catch { }
        }

        public static void CreateWaitingExecutionFile(int executionId)
        {
            var filePath = string.Format(WaitingExecutionPath, executionId);

            using (SecurityHelper.ImpersonateCitiUser.GetImpersonateUser(NetworkUser, NetworkUser, NetWorkPassword))
            {
                if (!File.Exists(filePath)) File.WriteAllText(filePath, "teste");
            }
        }

        public static void DeleteWaitingExecutionFile(int executionId)
        {
            var filePath = string.Format(WaitingExecutionPath, executionId);

            using (SecurityHelper.ImpersonateCitiUser.GetImpersonateUser(NetworkUser, NetworkUser, NetWorkPassword))
            {
                if (File.Exists(filePath)) File.Delete(filePath);
            }
        }

        public IEnumerable<RoutineExecutionHistory> SelectHistoryByRoutineExecution(int routineExecutionId)
        {
            return SelectAll<RoutineExecutionHistory>().Where(x => x.RoutineExecutionId == routineExecutionId);
        }
    }
}
